<?php

namespace app\models;

use think\Model;

/**
 * Class DepartmentModel
 */
class DepartmentModel extends Model
{
    protected $table = 'edu_department_table';

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return string
     */
    public static function getIds()
    {
        $campus = self::select()->toArray();
        $arr = [];
        foreach ($campus as $cap) {
            $arr[] = $cap['id'];
        }

        return implode(',', $arr);
    }

    /**
     * @param $id
     * @param string $type
     *
     * @throws \think\exception\DbException
     *
     * @return null|array|AuthGroupAccessModel
     */
    public static function getById($id, $type = 'all')
    {
        if (!is_numeric($id)) {
            return [];
        }
        $campus = self::get(['id' => $id]);
        if ($campus) {
            return (isset($campus[$type])) ? $campus[$type] : $campus;
        }

        return [];
    }

    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getDepartmentLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['department_name']) && '' !== $param['department_name']) {
            $where['department_name'] = ['like', $param['department_name'].'%'];
        }
        if (isset($param['status']) && '' !== $param['status']) {
            $where['status'] = $param['status'];
        }
        if(get_current_type() != 1){
			$where['company_id'] = get_current_company_id();
		}
        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['status_text'] = (1 === $item['status']) ? '启用' : '禁用';
                $item['company_name'] = CompanyModel::get($item['company_id'])['company_name'];
            }
        }

        return $listData;
    }

}
